<?php
class JoblistCtl extends BaseCtl {
	public function isPostback(){
		return 'joblist' == $this->getPost('pagename');
	}
	
    public function loadData(){
    	// we only load the selected stuff
    	$sel = $this->getPost('selected');
		$s = array();
    	
		if(is_array($sel)){
    		
    		foreach($sel as $current){
    			$s[$current] = 'sel';
    		}
    	}
		
    	$form = $this->getJoblistForm(); 
		$form->setSelected($s);
    }
    
    public function delete(){
    	try {
    		// check the selection contains at least
    		$form = $this->getJoblistForm();
    		
    		$selected = $form->getSelected();
    		if(count($selected) == 0){
    			throw new Exception('Please select at least one job to delete');
    		}
    		else {
    			$service = new Service();
    			
    			// get the ids from the selected stuff
    			$ids = array();
    			foreach($selected as $key => $value){
    				$ids[] = $key;
    			}
    			
    			$service->deleteJobs($ids);
    			$form->deleteJobs($ids);
    			
    			HtmlUtil::addMessage('Jobs deleted successfully');
    		}
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function add(){
    	try {
    		$service = new Service();
    		
    		$form = $this->getJobeditForm(); 
    		$form->clear();
    		
    		$form->setId($service->getNewId());
    		$form->setAdding(true);
    		$form->setStatusList($service->getJobStatusList()); 
    		
    		$this->redirect('jobedit.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function download(){
    	try {
    		$id = $this->getPost('idedit');
    		
    		$url = "jobdownload.php?id={$id}";
    		$this->redirect($url);
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function edit(){
    	try {
    		$service = new Service();
    		$statusList = $service->getJobStatusList();
    		
    		$form = $this->getJobeditForm(); 
    		$form->clear();
    		
    		$id = $this->getPost('idedit');
    		$job = $this->getJoblistForm()->getJob($id);

    		if($job == null){
    			throw new Exception('There is no job with the id: ' . $id);
    		}
    		
    		$form->setId($id);
    		$form->setAdding(false);
    		$form->setStatusList($statusList);
    		$form->setName($job->getName());
    		$form->setStatus($job->getStatus());
    		
    		$this->redirect('jobedit.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function selectlist(){
    	try {
    		$form = $this->getJobselectForm();
    		$frm = $this->getJoblistForm();
    		
    		$id = $this->getPost('idedit');
    		$job = $frm->getJob($id);
    		
    		if($job == null){
    			throw new Exception('Cannot find job with the id: ' . $id);
    		}
    		
    		// get the albums
    		$form->clear();
    		$service = new Service();
    		$userId = HtmlUtil::getSessionUser()->getUser()->getId(); 
    		
    		$albums = $service->searchAlbums($userId, '');
    		
    		$form->setJobId($job->getId());
    		$form->setJobName($job->getName());
    		$form->setAlbums($albums);
    		
    		$ids = $service->getJobPictures($job->getId());
    		$form->setJobPictures($ids);
    		
    		$this->redirect('jobselect.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
}

?>